// pages/user/user.js
var app = getApp()
var api = app.globalData.api
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      //获取是否登录
    var userInfo = wx.getStorageSync("userInfo");
    if (userInfo){
      console.log(userInfo);
      this.setData({
        user: userInfo
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /*返回上一页*/
  navigateBack: function () {
    var self = this;
    var pages = getCurrentPages();
    if (pages.length == 1) {
      if (self.data.circleId && self.data.circleId > 0) {
        wx.redirectTo({
          url: '../../circle/index/index?circleId=' + self.data.circleId
            + '&circleName=' + (self.data.circleName || '')
        });
      } else {
        wx.switchTab({
          url: "../../home/grouplist/grouplist"
        });
      }
    } else {
      wx.navigateBack({ changed: true });//返回上一页  
    }
  },
  //用户登录
  bindGetUserInfo: function (e) {
    var that = this;
    wx.login({
      success(res) {
        if (res.code) {
          var uinfo = e.detail.userInfo;
          uinfo.code = res.code;
          var formData = uinfo;
          //发起网络请求
          wx.request({
            url: api + '/user/user_info',
            header: {'content-type': 'application/x-www-form-urlencoded;charset=utf-8'},
            method: "post",
            data: formData,
            success: function (res) {
              if (res.data.code == 0) {
                wx.setStorageSync("userInfo", res.data.data);
                that.setData({
                  user: res.data.data
                })
              }
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 2000
              })
            }
          })
        } else {
          wx.showToast({
            title: res.errMsg,
            icon: 'none',
            duration: 2000})
        }
      }
    })
  },

//获取用户手机号
  getPhoneNumber:function(e){
    let that = this;
    var formData = e.detail
    formData.uid = that.data.user.id
    if (formData.errMsg == "getPhoneNumber:ok") {
      wx.request({
        url: api + '/user/user_tel',
        header: { 'content-type': 'application/x-www-form-urlencoded;charset=utf-8' },
        method: "post",
        data: formData,
        success: function (res) {
          if (res.data.code == 0) {
            wx.setStorageSync("userInfo", res.data.data);
            that.setData({
              user: res.data.data
            })
          }
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      })
    }else{
      wx.showToast({
        title: '获取失败',
        icon: 'none',
        duration: 2000
      })
    }
  } 

})